<?php
	class PrintPreviewController extends Controller
	{
		public $page_title = LANG_PRINT_PREVIEW;
		public $layout = 'print';
		public $helpers = array ('html', 'htmlx', 'toolbars', 'grids');
		public $name = 'PrintPreview';
		public $javascript = array('windows');
		public $checkAccess = false;

		/**
		 * defines whether the controller is to be cached
		 *
		 * @var boolean
		 */
		public $isCached = false;

		function index()
		{
			if (!empty($this->request->post))
			{
				$this->data[preview] = $this->request->post[print_preview];
				$this->data[title] = $this->request->post[report_title];
				$this->data[is_report] = $this->request->post[is_report];
				if($this->request->post[final_table]!=null)
				{
					$this->data[$this->name][step]=1;
					$this->data[preview] = $_POST[final_table];
					$this->data[title] = $_POST[title];
				}
			}
		}

		/**
		 * Prints the grid
		 *
		 */
		function Grid()
		{
			//grid title
			//$this->data[grid][title] = $this->request->get[title];
			$this->data[grid][title] = $_SESSION[grids][$this->request->get[name]][title];

			//grid header
			//$header = unserialize($this->request->get[header]);
			$header = $_SESSION[grids][$this->request->get[name]][header];
			$this->data[grid][header] = $header;

			//grid content
			//$data = unserialize($this->request->get[data]);
			$data = $_SESSION[grids][$this->request->get[name]][content];
			$this->data[grid][content] = $data;

			$this->data[grid][subTotalsBlock] = $_SESSION[grids][$this->request->get[name]][subtotals];
			$this->data[grid][totalsBlock] = $_SESSION[grids][$this->request->get[name]][totals];

			unset($_SESSION[grids][$this->request->get[name]]);

			//selected rows array
			if (!empty($this->request->get[selected])) $this->selectedRows = explode(',', $this->request->get[selected]);
			else $this->selectedRows = array();
		}

		/**
		 * Export the grid to the excel file
		 * @return unknown_type
		 */
		function GridExportToExcel()
		{
			//grid title
			$this->data[grid][title] = $_SESSION[grids][$this->request->get[name]][title];

			//grid header
			$header = $_SESSION[grids][$this->request->get[name]][header];
			$this->data[grid][header] = $header;

			//grid content
			$data = $_SESSION[grids][$this->request->get[name]][content];
			$this->data[grid][content] = $data;

			unset($_SESSION[grids][$this->request->get[name]]);

			$exportXLS = new SpreadsheetExport();
			$exportXLS->AddRow($this->data[grid][header]);

			foreach ($this->data[grid][content] as $elem)
			{
				$exportXLS->AddRow($elem);
			}

			$exportXLS->CreateFile(TEMP.SEPARATOR.'temp.xls', $this->data[grid][title].'.xls');
     		include(TEMP.SEPARATOR.'temp.xls');
		}

		/**
		 * Selects the print format
		 *
		 */
		function FormatSelection()
		{
			$this->printFormats = array
			(
				'0'=> array('id' => '17', 'name' => 'A4'),
				'1'=> array('id' => '24', 'name' => 'A3'),
				'2'=> array('id' => '12', 'name' => 'A5')
			);
		}
	}
?>